package com.qyhl.webtv.module_circle.circle.moment;


import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.launcher.ARouter;
import com.qyhl.webtv.basiclib.utils.DESedeUtil;
import com.qyhl.webtv.basiclib.utils.StringUtils;
import com.qyhl.webtv.basiclib.utils.network.EasyHttp;
import com.qyhl.webtv.basiclib.utils.network.callback.SimpleCallBack;
import com.qyhl.webtv.basiclib.utils.network.exception.ApiException;
import com.qyhl.webtv.commonlib.common.CommonUtils;
import com.qyhl.webtv.commonlib.constant.ServicePathConstant;
import com.qyhl.webtv.commonlib.entity.circle.CircleCommentBean;
import com.qyhl.webtv.commonlib.entity.circle.CircleHomeBean;
import com.qyhl.webtv.commonlib.entity.intergral.CoinBean;
import com.qyhl.webtv.commonlib.service.IntergralService;
import com.qyhl.webtv.module_circle.common.CircleUrl;
import com.qyhl.webtv.module_circle.common.CircleUtils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by qyhl on 2018/1/18.
 */

public class CircleMomentModel implements CircleMomentContract.CircleMomentModel {
    private CircleMomentPresenter mPresenter;

    @Autowired(name = ServicePathConstant.INTERGRAL_SERICE)
    public IntergralService intergralService;

    public CircleMomentModel(CircleMomentPresenter mPresenter) {
        this.mPresenter = mPresenter;
        ARouter.getInstance().inject(this);
    }

    /**
     * 获取动态内容
     *
     * @param username
     * @param lastTopicId
     */
    @Override
    public void getContent(String username, final String lastTopicId) {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        String time = sdf.format(new Date());
        String code = "Z5" + time + "I2863";
        String url = CircleUrl.CIRCLE_URL + "?timestamp=" + time;
        String userName = CommonUtils.getInstance().getUserName();

        EasyHttp.post(url)
                .upBytes(DESedeUtil.encryptMode(code, "siteId=" + CommonUtils.getInstance().getSite() + "&method=homePage" + "&username=" + userName + "&lastTopicId=" + lastTopicId))
                .isDecrypt(true)
                .decryptKet(code)
                .execute(new SimpleCallBack<List<CircleHomeBean>>() {
                    @Override
                    public void onError(ApiException e) {
                        if ("0".equals(lastTopicId)) {
                            mPresenter.setError(4, CircleUtils.getCircleMessage(e.getCode()));
                        } else {
                            mPresenter.setError(5, CircleUtils.getCircleMessage(e.getCode()));
                        }
                    }

                    @Override
                    public void onSuccess(List<CircleHomeBean> circleHomeBeans) {
                        if (circleHomeBeans != null && circleHomeBeans.size() > 0) {
                            if ("0".equals(lastTopicId)) {
                                mPresenter.setContent(circleHomeBeans, false);
                            } else {
                                mPresenter.setContent(circleHomeBeans, true);
                            }
                        } else {
                            if ("0".equals(lastTopicId)) {
                                mPresenter.setError(2, "暂无任何内容！");
                            } else {
                                mPresenter.setError(3, "no more");
                            }
                        }
                    }
                });


    }

    @Override
    public void sendComment(String topicId, String content, final String dstUsername) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        String time = sdf.format(new Date());
        String code = "Z5" + time + "I2863";
        String url = CircleUrl.CIRCLE_URL + "?timestamp=" + time;
        String userName = CommonUtils.getInstance().getUserName();
        
        
        EasyHttp.post(url)
                .upBytes(DESedeUtil.encryptMode(code, "siteId=" + CommonUtils.getInstance().getSite() + "&method=createPost" + "&username=" + userName + "&topicId=" + topicId
                        + "&content=" + content + "&reply=" + dstUsername
                ))
                .isDecrypt(true)
                .decryptKet(code)
                .execute(new SimpleCallBack<CircleCommentBean>() {
                    @Override
                    public void onError(ApiException e) {
                        if (e.getCode() == 155) {
                            mPresenter.commentError("您已经被管理员限制言论！");
                        } else {
                            mPresenter.commentError(CircleUtils.getCircleMessage(e.getCode()));
                        }
                    }

                    @Override
                    public void onSuccess(CircleCommentBean circleCommentBean) {
                        if (StringUtils.isNotEmpty(dstUsername)) {
                            mPresenter.commentSuccess(circleCommentBean.getPostId(), "评论成功！", 1);
                        } else {
                            mPresenter.commentSuccess(circleCommentBean.getPostId(), "评论成功！", 0);
                        }
                    }
                });
    }

    @Override
    public void getComCoin() {
        intergralService.gainCoin("7","gainCoin", new IntergralService.GainCoinCallBack() {
            @Override
            public void success(CoinBean coinBean) {
                mPresenter.getCoinSuccess(coinBean);
            }

            @Override
            public void error(String msg) {
                mPresenter.getCoinError(msg);
            }
        });
    }
}
